package com.lyl.serviceedu.controller.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.JwtUtils;
import com.lyl.commonutils.R;
import com.lyl.serviceedu.cloudclient.OrderClient;
import com.lyl.serviceedu.entity.Course;
import com.lyl.serviceedu.entity.EduTeacher;
import com.lyl.serviceedu.entity.vo.ChapterVo;
import com.lyl.serviceedu.entity.vo.CourseApiVo;
import com.lyl.serviceedu.entity.vo.CourseInfoVo;
import com.lyl.serviceedu.entity.vo.CoursePublishVo;
import com.lyl.serviceedu.query.CourseQuery;
import com.lyl.serviceedu.service.ChapterService;
import com.lyl.serviceedu.service.CourseService;
import com.lyl.serviceedu.service.EduTeacherService;
import com.lyl.vo.order.CourseWebVoOrder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-11
 */
@RestController
@RequestMapping("/serviceedu/api/course")
public class CourseApiController {

    @Autowired
    private CourseService courseService;


    @Autowired
    private ChapterService chapterService;

    @Autowired
    OrderClient orderClient;

    @Autowired
    EduTeacherService teacherService;


    /**
     * 返回公共类
     * @param courseId
     * @return
     */
    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CourseWebVoOrder getCourseInfoDto(@PathVariable Long courseId) {
        Course course = courseService.getById(courseId);
        EduTeacher teacher=teacherService.getById(course.getTeacherId());
        CourseWebVoOrder courseInfo = new CourseWebVoOrder();
        BeanUtils.copyProperties(course,courseInfo);
        courseInfo.setTeacherName(teacher.getName());
        return courseInfo;
    }


    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable("courseId") Long courseId,
            HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseApiVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        //远程调用，判断课程是否被购买
        R buyCourse = orderClient.isBuyCourse(Long.parseLong(JwtUtils.getMemberIdByJwtToken(request,"id")), courseId);


        System.out.println(buyCourse+":buyCourse");
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuy",buyCourse.getSuccess());
    }


    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery){
        Page<Course> pageParam = new Page<Course>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }
}

